package com.exp2.caloriedaily

import android.R
import android.annotation.SuppressLint
import android.app.Activity
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.get
import com.exp2.caloriedaily.adapter.CalorieListAdapter
import com.exp2.caloriedaily.domain.CalorieData
import com.exp2.caloriedaily.service.CalorieService
import kotlinx.android.synthetic.main.activity_manage_calorie.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.noButton
import org.jetbrains.anko.yesButton
import java.util.*


class ManageCalorieActivity : AppCompatActivity() , AdapterView.OnItemLongClickListener,View.OnClickListener {
    private var calorieService: CalorieService? =null
    var thisContext: Context? =null


    override fun onResume() {
        super.onResume()
        lv_calorie.adapter=CalorieListAdapter(this, calorieService!!.queryAll(), Color.WHITE)

    }
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(com.exp2.caloriedaily.R.layout.activity_manage_calorie)
        
        thisContext=this
        calorieService=CalorieService.getInstance(this)
        //calorieService!!.deleteAll()

        lv_calorie.adapter=CalorieListAdapter(this, calorieService!!.queryAll(), Color.WHITE)
        lv_calorie.onItemLongClickListener = this

        var update=findViewById<TextView>(com.exp2.caloriedaily.R.id.update)
        update.setOnClickListener(this)
    }

    override fun onItemLongClick(
        parent: AdapterView<*>?,
        view: View?,
        position: Int,
        id: Long
    ): Boolean {
        var data =calorieService!!.queryAll()
        alert("日期: ${data[position].date}    卡路里:${data[position].calorie}千卡") {
            title="确定删除?"
            yesButton {
                Log.d("data is", data[position].date!!)
                calorieService!!.delete("date='${data[position].date}'")
                lv_calorie.adapter=CalorieListAdapter(
                    thisContext!!,
                    calorieService!!.queryAll(),
                    Color.WHITE
                )
            }
           noButton { } }.show()
        return true
    }

    override fun onClick(v: View?) {
        val intent = Intent(this@ManageCalorieActivity, UpdateCalorieActivity::class.java)
        startActivity(intent)
    }

}